// JavaScript Document
function getloc(){
	navigator.geolocation.getCurrentPosition(
		function(pos){
			map.setCenter(new BMap.Point(pos.coords.longitude,pos.coords.latitude));
			setTimeout("$('#startup').fadeOut('fast');",1000);
		},
		function(err){
			$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js');
			getloc_ip(0);
		},{timeout:5000}
	);
};
function getloc_ip(i){
	if (i>3){
		map.centerAndZoom(new BMap.Point(116.404,39.915),15);
	}
	if (window.remote_ip_info){
		map.setCenter(window.remote_ip_info.city);
		$('#city').val(window.remote_ip_info.city);
		setTimeout("$('#startup').fadeOut('fast');",1000);
	} else {
		setTimeout("getloc_ip("+(i+1)+")",1000);
	};
};
function search1(){
	if ($('#search2').val()){
		map.clearOverlays();
		l = new BMap.LocalSearch(map,{renderOptions:{map:map,panel:'result'}});
		l.search($('#search2').val());
	}
};
$(function(){
	map = new BMap.Map("map");
	map.centerAndZoom(new BMap.Point(116.404,39.915),15);
	$('#map').css('position','fixed');
	if (navigator.geolocation){
		getloc();
	} else {
		$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js');
		getloc_ip(0);
	};
	map.addControl(new BMap.NavigationControl());  
	map.addControl(new BMap.ScaleControl());  
	map.addControl(new BMap.OverviewMapControl());  
	map.addControl(new BMap.MapTypeControl({anchor:BMAP_ANCHOR_TOP_LEFT,offset:new BMap.Size(60,10)}));
	map.enableScrollWheelZoom();
	map.enableKeyboard();
	map.enableContinuousZoom();
	$('#tabs').tabs();
	$('#btnSearch').button().click(search1);
	$('#tab2 a').css('color','blue').click(function(){
		$('#search2').val($(this).html());
		$('#btnSearch').click();
	});
	$('#btnClear1,#btnClear2').button().click(function(){
		map.clearOverlays();
		$('#result').html('');
		$('#search2,#start,#end').val('');
	});
	$('#btnSearch2').button().click(function(){
		map.clearOverlays();
		if ($('#route1').attr('checked')){
			t = new BMap.TransitRoute(map,{renderOptions:{map:map,panel:'result',autoViewport:true}});
		} else {
			t = new BMap.DrivingRoute(map,{renderOptions:{map:map,panel:'result',autoViewport:true}});
		}
		t.search($('#start').val(),$('#end').val());
	});
	$('#btncity').button().click(function(){
		map.setCenter($('#city').val());
	});
});